package com.itheima.springboot_start.CompetitionSystem;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class CompetitonManager {

    private List<Competition> competitions = new ArrayList<>();

    public boolean addCompetition(Competition competition) {
        if (isTimeConflict(competition)) {
            System.out.println("赛事日程冲突，无法添加该赛事！");
            return false;
        }
        competitions.add(competition);
        System.out.println("赛事信息发布：");
        System.out.println("赛事名称：" + competition.getName());
        System.out.println("开始时间：" + competition.getStartTime());
        System.out.println("结束时间：" + competition.getEndTime());
        System.out.println("赛事地点：" + competition.getLocation());
        return true;
    }

    public void deleteCompetition(String competitionName) {
        competitions.removeIf(competition -> competition.getName().equals(competitionName));
    }

    public Competition findCompetition(String competitionName) {
        for (Competition competition : competitions) {
            if (competition.getName().equals(competitionName)) {
                return competition;
            }
        }
        return null;
    }

    public List<Competition> getAllCompetitions() {
        return competitions;
    }

    private boolean isTimeConflict(Competition newCompetition) {
        LocalDateTime newStartTime = newCompetition.getStartTime();
        LocalDateTime newEndTime = newCompetition.getEndTime();
        for (Competition existingCompetition : competitions) {
            LocalDateTime existingStartTime = existingCompetition.getStartTime();
            LocalDateTime existingEndTime = existingCompetition.getEndTime();
            //时间不冲突
            if ((newStartTime.isBefore(existingEndTime) && newEndTime.isAfter(existingStartTime))) {
                return true;
            }
        }
        return false;
    }
}

